#!/bin/sh
function strstr()
{
        echo $1 | grep $2
}

function is_pub()
{
  res=$(strstr $(uname -r) "0009")
}

function include_link()
{
  res=$(strstr $(uname -r) "0009")
  res_4_14=$(strstr $(uname -r) "4.14")
  res_5_4=$(strstr $(uname -r) "5.4.203")
  res_5_4_new=$(strstr $(uname -r) "5.4.241-1")
  res_5_4_arm=$(strstr $(uname -r) "5.4.241-24")
  res_6_6=$(strstr $(uname -r) "6.6")
  if [ ! -z "$res" ]; then
          rm include
          ln -sf include_pub include
  elif [ ! -z "$res_5_4" ] || [ ! -z "$res_5_4_new" ]; then
          rm include
          ln -sf include_private include
  elif [ ! -z "$res_6_6" ]; then
	  rm include
	  ln -sf include_6_6 include
  elif [ ! -z "$res_4_14" ]; then
	  rm include
	  ln -sf include_tk3 include
  elif [ ! -z "$res_5_4_arm" ]; then
	  rm include
	  ln -sf include_tk4_arm include
  fi
}
function helper()
{
  echo -f, --function: "which scan from this function"
  echo -fg, --flags: "0:test hot path, 1: test one function, 2: test all syscall performance and throughput, 3: parameter optimize;"
  echo -ht, --hot: "only -fg 0/hot path case: first scan hot path, -ht 1, later -ht 0. other case -fg: no need assign -ht"
  echo -d, --dir: "only -fg 0 need assign -d: linux kernel source dir, uses server os kernel source code, or open source whoes version same as server os"
  echo -de, --delay: "duration, per function scan, default 5s;"
  echo -b, --block: "0: no block, 1: block"
  echo -o, --ondemand: "0:scan function ondemand; 1:scan function from all kernel dir; default 0"
  echo -ft, --fast: "0: trace one function once time, 1: trace all all sub function of parent function once time, default 1"
  echo -p, --proc: "only track the process assigned by proc, default: no proc assigned"
  echo -h, --help: "help information"
  echo such as:
  echo \(1\)test hotpath\(start form vfs_read, 1st time\):
  echo -e "\033[32m t-ops    os_stat   -d  /data/tkernel4\(or /data/linux, whoes version is the same as tkernel4\)  -f   vfs_read -fg 0  -ht  1  -de 5   -o  0 -ft 1 \033[0m"
  echo \(2\)test hotpath\(start form vfs_read, 2rd time\):
  echo -e "\033[32m t-ops    os_stat   -d  /data/tkernel4  -f   vfs_read -fg 0  -ht  0  -de 5   -o  0 -ft 1 \033[0m"
  echo \(3\)test one function\(vfs_read performance\):
  echo -e "\033[32m t-ops     os_stat  -f  vfs_read  -fg 1  -de 5  -o 0 \033[0m"
  echo \(4\)test all system performance:
  echo -e "\033[32m t-ops     os_stat   -fg 2   -de   1 \033[0m"
  echo \(5\)test   parameter   optimise:
  echo -e "\033[32m t-ops     os_stat   -fg 3   -de   5 \033[0m"
}
function main()
{
  POSITIONAL=()
  num=0

  PERFORMANCE="1"
  PROC="0"
  DELAY="5"
  BLOCK="0"

  while [[ $# -gt 0 ]]; do
  key="$1"
  num=$(($num+1))
  case $key in
      -d|--dir)
	DIR="$2"
	shift # past argument
	shift # past value
	;;
      -f|--function)
	FUNCTION="$2"
	shift # past argument
	shift # past value
	;;
      -fg|--flags)
	FLAGS="$2"
	shift # past argument
	shift # past value
	;;
      -ht|--hot)
	HOT="$2"
	shift # past argument
	shift # past value
	;;
      -de|--delay)
	DELAY="$2"
	shift # past argument
	shift # past value
	;;
      -b|--block)
	BLOCK="$2"
	shift # past argument
	shift # past value
	;;
      -o|--ondemand)
	ONDEMAND="$2"
	shift # past argument
	shift # past value
	;;
      -ft|--fast)
	PERFORMANCE="$2"
	shift # past argument
	shift # past value
	;;
      -p|--proc)
       PROC="$2"
       shift # past argument
       shift # past value
       ;;
      -h|--help)
	helper
	return
	shift # past argument
	;;
	*)
      POSITIONAL+=("$1") # save it in an array for later
	shift # past argument
	;;
    esac
  done

  #check paremter counts
  if [ $num -lt 2 ]; then
	helper
	return
  fi
 
  cd /usr/lib/opencloudos-tools/ops/os_stat/os_stat/

  #soft link include, pointer to kernel
  include_link

  #make modules
  res_6_6=$(strstr $(uname -r) "6.6")
  if [ ! -z "$res_6_6" ]; then
	make -C /usr/src/kernels/$(uname -r)   M=`pwd` modules version=6.6
  else :
	make -C /usr/src/kernels/$(uname -r)   M=`pwd` modules version=0
  fi
  mv os_aware.ko ../os_stat_user/

  #enter user dir
  cd /usr/lib/opencloudos-tools/ops/os_stat/os_stat_user

  #do the test
  res=$(is_pub)
  if [ ! -z "$res" ]; then
	  module=os_aware.ko
  else :
          tk4=$(strstr $(uname -r)  "5.4")
          if [ ! -z "$tk4" ]; then
                  tk4=${tk4:0:22}
		  module=os_aware_$tk4.ko.xz
          elif [ -f "os_aware_$(uname -r).ko.xz" ] ; then
		  module=os_aware_$(uname -r).ko.xz
	  fi
  fi

  if [ ! -z "$res" ] || [ ! -f "$module" ]; then
    cd /usr/lib/opencloudos-tools/ops/os_stat/os_stat/
    #soft link include, pointer to kernel
    include_link

    #make modules
    res_6_6=$(strstr $(uname -r) "6.6")
    if [ ! -z "$res_6_6" ]; then
      make -C /usr/src/kernels/$(uname -r)   M=`pwd` modules version=6.6
    else :
      make -C /usr/src/kernels/$(uname -r)   M=`pwd` modules version=0
    fi
    mv os_aware.ko ../os_stat_user/
    cd /usr/lib/opencloudos-tools/ops/os_stat/os_stat_user
  fi

  make

  if [ ! -f "$module" ] ; then
	  module=os_aware.ko
  fi

  if [ $FLAGS -eq 0 ]; then
	  if [ ! -z "$ONDEMAND" ]; then
	    ./stat.sh $module $DIR $FUNCTION $HOT $DELAY $ONDEMAND 1 $PERFORMANCE $PROC
	  else :
	    ./stat.sh $module $DIR $FUNCTION $HOT $DELAY 0 1 $PERFORMANCE $PROC
	  fi
  else :
	  insmod $module
	  if [ $FLAGS -eq 1 ]; then
	      IFS=,; for SUBFUNCTION in $FUNCTION; do
	      echo $SUBFUNCTION;
	      ./os_stat_blongm -f $SUBFUNCTION -1 tmp -2 tmp -n 1 -h 0 -t $DELAY -i 0 -b $BLOCK -x;
	      done
	  elif [ $FLAGS -eq 2 ] ; then
	      ./os_stat_blongm -f tmp -1 tmp -1 tmp -i 1 -t $DELAY

	  else :
	      cd /usr/lib/opencloudos-tools/ops/os_stat/os_stat_paremter
	      ./paremter_main.py $DELAY
	  fi
          rmmod os_aware
  fi

}

main $*
